#include "testDrive.h"
#include<time.h> 

#define performance_value 100

void performanceTestRun( string str, clock_t t1, clock_t t2, int line){
	float diff;
	ostringstream oss;

	diff = (((float)t2 - (float)t1) / 1000000.0F ) * 1000;
	diff /= performance_value;
	oss << str << ": time: " <<  diff << " sec";
	logger::success(oss.str(), "performanceTest.txt", line);
}

void performanceTest(big_number *num1, big_number *num2, int line)
{
	big_number tmp1, tmp2;
	int i;
	clock_t t1, t2;
	
	printf("Start performance test + \n");
	t1 = clock();   
	for(i = 0;i < performance_value ;i++)   
	{
		(*num1)+(*num2);
	}   
	t2 = clock();
	performanceTestRun( (*num1).toString() + " + " + (*num2).toString(), t1, t2, line);

	printf("Start performance test - \n");
	t1 = clock();   
	for(i = 0;i < performance_value ;i++)   
	{
		(*num1)-(*num2);
	}   
	t2 = clock();
	performanceTestRun( (*num1).toString() + " - " + (*num2).toString(), t1, t2, line);

	printf("Start performance test * \n");
	t1 = clock();   
	for(i = 0;i < performance_value ;i++)   
	{
		(*num1)*(*num2);
	}   
	t2 = clock();
	performanceTestRun( (*num1).toString() + " * " + (*num2).toString(), t1, t2, line);

	printf("Start performance test / \n");
	t1 = clock();   
	for(i = 0;i < performance_value ;i++)   
	{
		(*num1)/(*num2);
	}   
	t2 = clock();
	performanceTestRun( (*num1).toString() + " / " + (*num2).toString(), t1, t2, line);

	printf("Start performance test MOD \n");
	big_integer bi1((*num1));
	big_integer bi2((*num2));
	t1 = clock();  
	for(i = 0;i < performance_value ;i++)   
	{
		bi1%bi2;
	}   
	t2 = clock();
	performanceTestRun( bi1.toString() + " % " + bi2.toString(), t1, t2, line);

	printf("Start performance test sqrt \n");
	t1 = clock();   
	for(i = 0;i < performance_value ;i++)   
	{
		(*num1).SQRT();
	}   
	t2 = clock();
	performanceTestRun( (*num1).toString() + " SQRT ", t1, t2, line);

}


void performanceParser(string *arr, big_number **a, big_number **b){
	if ( arr[0].compare("i") == 0)
		*a = new big_integer(arr[1]);
	else
		*a=  new big_number(arr[1]);

	if ( arr[2].compare("i") == 0)
		*b = new big_integer(arr[3]);
	else
		*b =  new big_number(arr[3]);
}

void performanceTest(){
	PNode tmp, ptr = readFile("performanceTest.txt");
	string arr[4];
	big_number *a, *b;
	while ( ptr != NULL ){
		try { 
			splitByComma(arr, ptr->line);
			performanceParser(arr, &a, &b);
			performanceTest(a, b, ptr->lineNumber);
		} 
		catch( const std::exception &e) {
			cout << e.what() << endl;
				logger::exception(ptr->line, "performanceTest.txt", ptr->lineNumber);
		}
		tmp = ptr;
		ptr = ptr->next;
		freeNode(tmp);
	}
}